package cc.customer.web.saleHelp;

import cc.customer.ServicesUtils.NoticeUtils;
import cc.customer.common.domain.QueryRequest;
import cc.customer.common.domain.ResponseBo;
import cc.customer.common.utils.Constant;
import cc.customer.common.utils.DateUtil;
import cc.customer.common.utils.Utils;
import cc.customer.rabbit.domain.CconvNotice;
import cc.customer.saleHelp.domain.CsaleModal;
import cc.customer.saleHelp.domain.CsalePlan;
import cc.customer.saleHelp.services.CsaleModalServices;
import cc.customer.saleHelp.services.CsalePlanServices;
import cc.customer.sendMsg.SendMagRecevicer;
import cc.customer.system.domain.MyUser;
import cc.customer.web.sys.controller.base.BaseController;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping(value = "SaleHelp")
public class SaleHelpController extends BaseController {

    @Autowired
    private CsaleModalServices csaleModalServices;

    @Autowired
    private CsalePlanServices  csalePlanServices;
    @Autowired
    private SendMagRecevicer sendMagRecevicer;


    @RequestMapping(value = "/index")
    public String index(){
        return "customer/saleHelp/saleHelp_index";
    }

    /**新建计划**/
    @RequestMapping(value = "/loadAddSalePlan")
    public String loadAddSalePlan(){return "customer/saleHelp/saleHelp_add"; }
    /**新建个人分组**/
    @RequestMapping(value = "/loadperSonGroup")
    public String loadperSonGroup(){ return "customer/saleHelp/option/person_group"; }
    /**新建个人分组**/
    @RequestMapping(value = "/loadConnyGroup")
    public String loadConnyGroup(){ return "customer/saleHelp/option/company_group"; }
    /**新建个人模板**/
    @RequestMapping(value = "/loadPersonModal")
    public String loadPersonModal(){return "customer/saleHelp/option/person_modal";}

    /**修改个人模板分组**/
    @RequestMapping(value = "/editPersonModal")
    public String editPersonModal(){return "customer/saleHelp/option/person_edit";}

    /**新建企业模板**/
    @RequestMapping(value = "/loadCompnyModal")
    public String loadCompnyModal(){return "customer/saleHelp/option/compny_modal";}

    /**修改企业模板分组**/
    @RequestMapping(value = "/editCompnyModal")
    public String editCompnyModal(){return "customer/saleHelp/option/compny_edit";}

    /**个人模板详情**/
    @RequestMapping(value = "/personModalDetail")
    public String personModalDetail(){return "customer/saleHelp/person_form";}
    /**企业模板详情**/
    @RequestMapping(value = "/compnyModalDetail")
    public String compnyModalDetail(){return "customer/saleHelp/compny_form";}


    /**新建模板分组**/
    @RequestMapping(value = "/addModalGroup")
    @ResponseBody
    public ResponseBo addModalGroup(CsaleModal csaleModal){
        MyUser user  = this.getCurrentUser();
        csaleModal.setcSaleCtime(DateUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
        csaleModal.setcSaleUserUuid(user.getUserUuid());
        csaleModal.setcSaleUuid(Utils.getUUID());
        csaleModal.setcSaleDelete("1");
        return csaleModalServices.addModalGroup(csaleModal);
    }
    /**初始化模板列表**/
    @RequestMapping(value = "/getModalGrouplist")
    @ResponseBody
    public ResponseBo getModalGrouplist(CsaleModal csaleModal){
        MyUser user  = this.getCurrentUser();
        csaleModal.setcSaleUserUuid(user.getUserUuid());
        return csaleModalServices.selectModalList(csaleModal);
    }

    /**模板的上移下移**/
    @RequestMapping(value = "/upDownOption")
    @ResponseBody
    public ResponseBo upDownOption(String beforUuid,String afterUuid){
        return csaleModalServices.upDownOption(beforUuid,afterUuid);
    }
    /**模板更新信息详情**/
    @RequestMapping(value = "/updateModalInfo")
    @ResponseBody
    public ResponseBo updateModalInfo(CsaleModal csaleModal){
        return csaleModalServices.updateModalInfo(csaleModal);
    }

    /**模板更新详细的内容(时间格式信息)**/
    @RequestMapping(value = "/updateModalDetail")
    @ResponseBody
    public ResponseBo updateModalDetail(CsaleModal csaleModal){
        /**判断时间类型**/
        try {
            String cSaleTime = csaleModal.getcSaleTime();
            JSONObject json  = new JSONObject();
            switch (cSaleTime){
                case Constant.CSALEMODAL_TIME_DAY:
                    json.put("d",csaleModal.getOneParame());json.put("t",csaleModal.getThreeParame());
                    break;
                case Constant.CSALEMODAL_TIME_WEEK:
                    json.put("wc",csaleModal.getOneParame());json.put("w",csaleModal.getTwoParame());
                    json.put("t",csaleModal.getThreeParame());
                    break;
                case Constant.CSALEMODAL_TIME_MONTH:
                    json.put("mc",csaleModal.getOneParame());json.put("md",csaleModal.getTwoParame());
                    json.put("t",csaleModal.getThreeParame());
                    break;
                case Constant.CSALEMODAL_TIME_HOUR:
                    json.put("h",csaleModal.getOneParame());
                    break;
                default:
                    json.put("t",csaleModal.getOneParame());
            }
            csaleModal.setcSaleJson(json.toJSONString());
        }catch (Exception e){
            e.printStackTrace();
            return ResponseBo.error();
        }
        return csaleModalServices.updateModalInfo(csaleModal);
    }

    /**获取模板的详细信息**/
    @RequestMapping(value = "/getModalInfoDetail")
    @ResponseBody
    public ResponseBo getModalInfoDetail(CsaleModal csaleModal){
        return csaleModalServices.selectModalInfo(csaleModal);
    }

    /**根据模板的分组加载当前分组下的 所有 的模板**/
    @RequestMapping(value = "/selectAllModalByUuid")
    @ResponseBody
    public ResponseBo selectAllModalByUuid(CsaleModal csaleModal){
        return csaleModalServices.selectModalByUuid(csaleModal);
    }

    /**获取模板的详细信息  解析完时间**/
    @RequestMapping(value = "/getModalDetail")
    @ResponseBody
    public ResponseBo getModalDetail(String saleUUid){
        return  csaleModalServices.selectModalAndPraseTime(saleUUid);
    }



    /*********************************新建个人计划**************************************/
    /**选择我的客户的信息**/
    @RequestMapping(value = "/getScalePlanCus")
    public String getScalePlanCus(){
        return "customer/saleHelp/add_plan/select_user";
    }
    /**选择我的客户的模板**/
    @RequestMapping(value = "/getScalePlanModal")
    public String getScalePlanModal(){
        return "customer/saleHelp/add_plan/select_modal";
    }

    /**点击加载模板详情**/
    @RequestMapping(value = "/getModalDetailPsrasetime")
    public String getModalDetailPsrasetime(){
        return "customer/saleHelp/add_plan/modal_detail";
    }

    /**点击加载计划详情的编辑**/
    @RequestMapping(value = "/getScalePlanDetailsd")
    public String getScalePlanDetailsd(){
        return "customer/saleHelp/saleHelp_edit";
    }

    /**点击加载计划客户列表**/
    @RequestMapping(value = "/loadPlanCus")
    public String loadPlanCus(){
        return "customer/saleHelp/saleHelp_cusList";
    }

    /**保存销售计划**/
    @RequestMapping(value = "/saveScalePlan")
    @ResponseBody
    public ResponseBo saveScalePlan(CsalePlan csalePlan){
        /**验证时间**/
        String time= csalePlan.getcPlanTime();
        Date dateNo =  new Date();
        Date date  = DateUtil.convert2Date(time,"yyyy-MM-dd HH:mm");
        Long inum  = DateUtil.diffTimes(dateNo,date);
        if (inum <= 0){
            return ResponseBo.error("执行时间不能小于当前时间");
        }
        MyUser user = this.getCurrentUser();
        String userid = user.getUserUuid();
        csalePlan.setcPlanUserid(userid);

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
            String type = csalePlan.getcPlanType();
            CconvNotice notice = NoticeUtils.planSaleNotice(type,user);
            sendMagRecevicer.processUser(notice);
            }
        }, date);
        try {
            return csalePlanServices.saveCsalePlan(csalePlan);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseBo.error();
        }

    }

    /**查询个人的工作计划模板**/
    @RequestMapping(value = "/selectScalePlan")
    @ResponseBody
    public ResponseBo selectScalePlan(CsalePlan csalePlan){
        MyUser user = this.getCurrentUser();
        String userid = user.getUserUuid();
        csalePlan.setcPlanUserid(userid);
        return csalePlanServices.selectAllMyCsalePlan(csalePlan);
    }

    /**查询销售计划详情**/
    @RequestMapping(value = "/selectSacleDetail")
    @ResponseBody
    public ResponseBo selectSacleDetail(CsalePlan csalePlan){
        return csalePlanServices.selectAMyCsalePlanDetail(csalePlan);
    }


    /**查询销售计划详情**/
    @RequestMapping(value = "/getPlanCusList")
    @ResponseBody
    public ResponseBo getPlanCusList(CsalePlan csalePlan){
        return csalePlanServices.selectPlanCusList(csalePlan);
    }

    /**编辑保存**/
    @RequestMapping(value = "/editScalePlanforupdate")
    @ResponseBody
    public ResponseBo editScalePlanforupdate(CsalePlan csalePlan){
        /**验证时间**/
        String time= csalePlan.getcPlanTime();
        Date dateNo =  new Date();
        Date date  = DateUtil.convert2Date(time,"yyyy-MM-dd HH:mm");
        Long inum  = DateUtil.diffTimes(dateNo,date);
        if (inum <= 0){
            return ResponseBo.error("执行时间不能小于当前时间");
        }
        return csalePlanServices.updateMyCsaleDetail(csalePlan);
    }

    /**编辑删除**/
    @RequestMapping(value = "/editScalePlanfordelete")
    @ResponseBody
    public ResponseBo editScalePlanfordelete(CsalePlan csalePlan){
        return csalePlanServices.updateMyCsaleDetail(csalePlan);
    }
    /**查询客户的销售计划**/
    @RequestMapping(value = "/selectScalePlanByUserUUid")
    @ResponseBody
    public ResponseBo selectScalePlanByUserUUid(String userUuid,String isImpl){
        return csalePlanServices.selectSalePlanByUserUuid(userUuid,isImpl);
    }

    /**删除客户的销售几哈**/
    @RequestMapping(value = "/deleteScalePlanByUseridUuid")
    @ResponseBody
    public ResponseBo deleteScalePlanByUseridUuid(String palnId, String userUuid,String types){
        return csalePlanServices.deleteSaclePlanUserUuid(palnId,userUuid,types);
    }


    /**初始化各种类型的数量**/
    @RequestMapping(value = "/getTypeNum")
    @ResponseBody
    public ResponseBo getTypeNum(){
        return csalePlanServices.getTypeNum();
    }


}
